package com.tledu.spring.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.spring.core.entity.AjaxResult;
import com.tledu.spring.domain.Craft;
import com.tledu.spring.domain.Plan;
import com.tledu.spring.service.ICraftService;
import com.tledu.spring.service.IPlanService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@AllArgsConstructor
@RequestMapping("/craft/plan")
public class PlanController {
    private IPlanService planService;
    private ICraftService craftService;
    @RequestMapping()
    public String list(Plan plan, Model model) {
        model.addAttribute("list", planService.query(plan));
        return "craft/plan/list";
    }
    @PostMapping("list")
    @ResponseBody
    public AjaxResult<PageInfo<Plan>> listAjaxResult(Plan plan, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Plan> list = planService.query(plan);
        PageInfo<Plan> pageInfo = new PageInfo<>(list);
        return new AjaxResult<PageInfo<Plan>>(pageInfo);
    }

    @GetMapping("/add")
    public String add(Craft craft,Model model) {
        List<Craft> lists = craftService.selectByAll(craft);
        model.addAttribute("list",lists);
        return "craft/plan/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult<String> add(Plan plan) {
        planService.insert(plan);
        //System.out.println(plan);
        return new AjaxResult<>("新增成功");
    }

    @DeleteMapping("/delete/{pl_id}")
    @ResponseBody
    public AjaxResult<String> delete(@PathVariable Integer pl_id) {
        planService.delete(pl_id);
        return new AjaxResult<>("删除成功");
    }

    @GetMapping("/edit")
    public String edit(Integer pl_id,Model model) {
        model.addAttribute("plan", planService.selectByPl_Id(pl_id));
        return "craft/plan/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult<String> edit(Plan plan) {
        planService.update(plan);
        return new AjaxResult<>("修改成功");
    }
}
